﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class Tab_ItemDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="itemID">标识列ID</param>
		public bool Exists(int itemID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [Tab_Item] where itemID=@itemid ");
			htSyn.Add("@itemid", itemID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<Tab_Item> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [itemID],[itemName],[itemKey],[itemInfo] FROM [Tab_Item]");
			 return getTab_ItemBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="itemID">标识列ID</param>
		/// <returns></returns>
		public Tab_Item GetModel(int itemid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [itemID],[itemName],[itemKey],[itemInfo] FROM [Tab_Item]");
			strSql.Append(" where itemID=@itemid ");
			htSyn.Add("@itemid", itemid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			Tab_Item tab_itemModel = new Tab_Item();
			if (dt.Rows.Count > 0)
			{
				tab_itemModel.itemName=dt.Rows[0]["itemName"].ToString();
				tab_itemModel.itemKey=dt.Rows[0]["itemKey"].ToString();
				tab_itemModel.itemInfo=dt.Rows[0]["itemInfo"].ToString();
			}
			return tab_itemModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="Tab_Item">实体</param>
		/// <returns></returns>
		public bool Update(Tab_Item tab_itemModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [Tab_Item] set ");
			strSql.Append("[itemName]=@itemname,");
			strSql.Append("[itemKey]=@itemkey,");
			strSql.Append("[itemInfo]=@iteminfo");

			strSql.Append(" where itemID=@itemid ");
			htSyn.Add("@itemid", tab_itemModel.itemID);
			htSyn.Add("@itemname", tab_itemModel.itemName);
			htSyn.Add("@itemkey", tab_itemModel.itemKey);
			htSyn.Add("@iteminfo", tab_itemModel.itemInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="itemID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int itemID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [Tab_Item] where itemID=@itemid");
			htSyn.Add("itemid", itemID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="Tab_Item">实体</param>
		/// <returns></returns>
		public bool Insert(Tab_Item tab_itemModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [Tab_Item]([itemName],[itemKey],[itemInfo]) values (@itemname,@itemkey,@iteminfo)");
			htSyn.Add("@itemname", tab_itemModel.itemName);
			htSyn.Add("@itemkey", tab_itemModel.itemKey);
			htSyn.Add("@iteminfo", tab_itemModel.itemInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<Tab_Item> getTab_ItemBySql(string strSql)
		{
			IList<Tab_Item> list = new List<Tab_Item>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Tab_Item tab_itemModel = new Tab_Item();
					tab_itemModel.itemID= Convert.ToInt32(dr["itemID"]);
					tab_itemModel.itemName=dr["itemName"].ToString();
					tab_itemModel.itemKey=dr["itemKey"].ToString();
					tab_itemModel.itemInfo=dr["itemInfo"].ToString();
					list.Add(tab_itemModel);
				}
			}
			return list;
		}
		#endregion
	}
}
